package ut.service;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Mail implements Runnable{
	public static void sendDailyUpdateEmail(int updatedCount, String content){
		Mail.sendAsynchronous("padalab@gmail.com,nizniz187@gmail.com", "每日更新報告：" + updatedCount, content);
	}
	public static void sendAbuseReportEmail(){
		Mail.sendAsynchronous("padalab@gmail.com,nizniz187@gmail.com", "使用者申訴", "有使用者提出申訴，請盡快處理。");
	}
	public static boolean sendContactEmail(ut.data.User user, String subject, String content){
		return Mail.send("padalab@gmail.com,nizniz187@gmail.com", "使用者意見：" + subject, "來自使用者 <a href='http://ut.pada-x.com/account/member-info.jsp?uid=" + user.id + "'>" + user.name + "</a> 的訊息：<br/><br/>" + content);
	}
	public static void sendNotifyEmail(String address, String name, ut.res.MessageType messageType){
		Mail.sendAsynchronous(address, "UT 終極一翻 - " + messageType.subject,
			"您好，" + name + "：<br/><br/>UT 終極一翻的站務系統發送了一則【" + messageType.subject + "】通知給您，請務必登入<a href='http://ut.pada-x.com/'>UT 終極一翻</a>的個人訊息匣查看詳情。");
	}
	public static void sendVerifyEmail(int id, String address, String name, String verifyCode){
		String subject = "UT 終極一翻 - 電子信箱驗證";
		StringBuilder content = new StringBuilder("您好，" + name + "<br/><br/>");
		content.append("請<a href='http://ut.pada-x.com/exe/Verify?source=email&id=" + id + "&code=" + verifyCode + "'>點擊此處</a>或複製以下連結至您的瀏覽器，以完成電子信箱驗證。<br/>" + "http://ut.pada-x.com/exe/Verify?source=email&id=" + id + "&code=" + verifyCode + "<br/><br/>");
		content.append("進行完驗證後，就可以在【UT 終極一翻】開始進行接案或發案了，祝您一切順利。<br/><br/>-----------<br/><br/>UT 終極一翻 http://ut.pada-x.com/");
		Mail.sendAsynchronous(address, subject, content.toString());
	}
	public static void sendAsynchronous(String addresses, String subject, String content){
		(new Thread(new Mail(addresses, subject, content))).start();
	}
	private static boolean send(String addresses, String subject, String content){
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");

		Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator(){
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("ut@pada-x.com", "ut123456");
			}
		});
 
		try{
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress("ut@pada-x.com", "UT 終極一翻"));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(addresses));
			message.setSubject(subject, "utf-8");
			message.setText(content, "utf-8", "html");
			Transport.send(message);
			return true;
		}catch(MessagingException e){
			e.printStackTrace();
			return false;
		}catch(java.io.UnsupportedEncodingException e){
			e.printStackTrace();
			return false;
		}
	}
	/* Instance Definition */
	public String addresses;
	public String subject;
	public String content;
	public Mail(String addresses, String subject, String content){
		this.addresses = addresses;
		this.subject = subject;
		this.content = content;
	}
	public void run(){
		Mail.send(this.addresses, this.subject, this.content);
	}
}